package main;

import main.cipher.TokenUtils;
import org.springframework.web.util.WebUtils;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

public class RequestHelper {
    public static String getUserUUIDFromRequest(HttpServletRequest request) {
        String tokenStr = null;
        Cookie tokenCookie = WebUtils.getCookie(request, "token");
        if (tokenCookie != null) {
            tokenStr = tokenCookie.getValue();
        }
        String uuid = null;
        if (tokenStr != null) {
            uuid = TokenUtils.getUUIDFromToken(tokenStr);
        }
        return uuid;
    }

    public static String[] getAccountInfosFromRequest(HttpServletRequest request) {
        String accountToEvaluate = null, encryptedKey = null;
        Map<String, String[]> map = request.getParameterMap();
        if (map.containsKey("account")) {
            accountToEvaluate = map.get("account")[0];
        }
        if (map.containsKey("key")) {
            encryptedKey = map.get("key")[0];
        }

        String[] result = new String[2];
        result[0] = accountToEvaluate;
        result[1] = encryptedKey;

        return result;
    }
}
